package work5.CourseManagement;

import Uti.ScannerUti;
import work5.StudentInfManagement.Student;
import work5.StudentInfManagement.StudentDao;

import java.util.Iterator;
import java.util.List;

public class CourseManage {
    public CourseManage() {

        while (true) {
            System.out.println("课程信息管理");
            System.out.println("--------------------------------");
            System.out.println("课程信息添加————1");
            System.out.println("课程信息修改————2");
            System.out.println("课程信息删除————3");
            System.out.println("课程信息查询————4");
            System.out.println("退出————0");
            int kk = ScannerUti.getInt();
            if (kk == 1) {
                addCourse();
            } else if (kk == 3) {
               delete();
            } else if (kk == 2) {
                edit();
            } else if (kk == 4) {
                search();
            } else if (kk == 0) {
                break;
            }
        }
    }


    public static void addCourse()  {
        System.out.println("请输入课程名称, 学分 以空格隔开");
        String[] s = (ScannerUti.getSting().split(" "));
        if(s.length<2){
            System.out.println("最后一条数据信息 输入有误");
            return;
        }
        Course course= null;
        String name = s[0];
        String credit = s[1];
        course = new Course(0, name, Double.parseDouble(credit));
        CourseDao.insert(course);

        printCourses(CourseDao.find(new Course(0)));

    }


    //学生信息的删除

    public static void delete() {
        System.out.println("请输入课程编号：");
        int id = ScannerUti.getInt();
        List<Course> courses = CourseDao.find(new Course(id));
        if(courses.size()==0){
            System.out.println("没有这个课程！");
        }
        else {
            CourseDao.delete(new Course(id));
            printCourses(CourseDao.find(new Course(0)));
        }
    }

//一个学生信息的编辑

    public static void edit() {
        System.out.println("请输入课程编号：");
        int id = ScannerUti.getInt();
        List<Course> courses = CourseDao.find(new Course(id));

        if(courses.size()==0){
            System.out.println("没有这个课程！");
        }
        else {
            Course s=courses.get(0);
            System.out.println("请输入课程 名称，学分（空格隔开）");
            String[] s1 = (ScannerUti.getSting().split(" "));
            s.setName(s1[0]);
            s.setCredit(Double.parseDouble(s1[1]));
            CourseDao.change(s);
            printCourses(CourseDao.find(new Course(0)));
        }
    }

//学生信息的查询显示

    public static void search() {
        System.out.println("请输入查询条件(空格查询所有):");
        String s = ScannerUti.getSting();
        if(s.equals(" ")){
            printCourses(CourseDao.find(new Course(0)));
        }else{
            int key =Integer.parseInt(s);
            printCourses(CourseDao.find(new Course(key)));
        }


    }
//学生信息的打印显示

    public static void printCourses(List<Course> results) {
        System.out.println("编号\t\t姓名\t\t专业");
        System.out.println("--------------------------------");
        if (results.size() > 0) {
            Iterator<Course> iterator = results.iterator();
            while (iterator.hasNext()) {
                Course s = iterator.next();
                System.out.println(s);
            }
        } else {
            System.out.println("没有学生信息！！");
        }
        System.out.println("--------------------------------");
    }

}
